﻿using Authentication.Web.App_Start;
using Common.Data;
using Common.Service.DataTables;
using System;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace Authentication.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801
    public class MvcApplication : System.Web.HttpApplication
    {
        private WebDbContextStorage _storage;
        public override void Init()
        {
            base.Init();
            _storage = new WebDbContextStorage(this);
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            ModelBinders.Binders.Add(typeof(DataTablesPageRequest), new DataTablesModelBinder()); 
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            DbContextInitializer.Instance().InitializeDbContextOnce(() =>
            {
                DbContextManager.InitStorage(_storage);
                DbContextManager.Init(new[] { Server.MapPath("~/bin/Authentication.dll") }, false);
            });
        }
    }
}